#include "stm32f10x.h"	    
#include "touch.h"   
#include "LCD_ILI9325.h"
#include "stdio.h"
#include "config.h"



screenZone GetScreenZone()
{
    int X,Y;

    char mes[32];    

    X = GUI_TOUCH_X_MeasureX();
    Y = GUI_TOUCH_X_MeasureY();
        
  //  X = X & 0xFFF;
  //  Y = Y & 0xFFF;

    sprintf(mes, "X = %d, Y = %d\r\n", X, Y);
    printf(mes);
    //ILI9325_DisplayStringLine(0, mes);

    X -= 0x100;
    Y -= 0x100;


/*    if ( X > 0xB3D && X < 0xFFF && Y > 0 && Y < 0x3BF ) 
        return zone1;
    else if ( X > 0xB3D && X < 0xFFF && Y > 0x3BF && Y < 0x77E ) 
        return zone2; 
    else if ( X > 0xB3D && X < 0xFFF && Y > 0x77E && Y < 0xB3D ) 
        return zone3; 
    else if ( X > 0xB3D && X < 0xFFF && Y > 0xB3D && Y < 0xFFF ) 
        return zone4; 

    else if ( X > 0x3BF && X < 0xB3D && Y > 0 && Y < 0x3BF ) 
        return zone_FG;
    else if ( X > 0x3BF && X < 0xB3D && Y > 0x3BF && Y < 0xB3D ) 
        return zone_ICONE; 
    else if ( X > 0x3BF && X < 0xB3D && Y > 0xB3D && Y < 0xFFF ) 
        return zone_FD; 

    else if ( X > 0 && X < 0x3BF && Y > 0 && Y < 0x3BF ) 
        return zone8;
    else if ( X > 0 && X < 0x3BF && Y > 0x3BF && Y < 0x77E ) 
        return zone9; 
    else if ( X > 0 && X < 0x3BF && Y > 0x77E && Y < 0xB3D ) 
        return zone10; 
    else if ( X > 0 && X < 0x3BF && Y > 0xB3D && Y < 0xFFF ) 
        return zone11; 
    else
        return noz;*/


    SpiDelay(100000);
    
    return 0;
}


void touch_init()
{
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    //RCC_PCLK2Config(RCC_HCLK_Div2);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_Configuration();	

    /* Configure one bit for preemption priority */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

    /* Enable the EXTI9_5 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn  ;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource6);

    /* Configure Key Button EXTI Line to generate an interrupt on falling edge */  
    EXTI_InitStructure.EXTI_Line = EXTI_Line6;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    SPI_config();
    UARTs_Config();
}	

void SPI_config()
{
    SPI_InitTypeDef   SPI_InitStructure;

	// SPI1 Config
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;   //SPI_NSS_Hard
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(SPI1,&SPI_InitStructure);

	// SPI1 enable
	SPI_Cmd(SPI1,ENABLE);
    UARTs_Config();
}

void GPIO_Configuration()
{
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;          // CS
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;		
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    //Configure SPI1 pins: SCK, MISO and MOSI
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
	GPIO_Init(GPIOA,&GPIO_InitStructure);
   
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;  //PENIRQ
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}


unsigned char SPI_WriteByte(unsigned char data)
{
 unsigned char Data = 0;

   //Wait until the transmit buffer is empty
  while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
  // Send the byte
  SPI_I2S_SendData(SPI1,data);

   //Wait until a data is received
  while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
  // Get the received data
  Data = SPI_I2S_ReceiveData(SPI1);

  // Return the shifted data
  return Data;
}

void SpiDelay(unsigned int DelayCnt)
{
	unsigned int i;
	for(i=0;i<DelayCnt;i++);
}

u16 readAmbiantTemp()
{
   u16 temp=0;

   SPI_TOUCH_CS_LOW();

   SpiDelay(10);
   SPI_WriteByte(0x80);
   SpiDelay(10);
   temp=SPI_WriteByte(0x00);
   temp<<=8;
   temp+=SPI_WriteByte(0x00);
   SpiDelay(10);

   SPI_TOUCH_CS_HIGH();

   temp = temp>>3;
   return (temp);
}

u16 TPReadX(void)
{
   u16 x=0;

   SPI_TOUCH_CS_LOW();

   SpiDelay(10);
   SPI_WriteByte(0x90);
   SpiDelay(10);
   x=SPI_WriteByte(0x00);
   x<<=8;
   x+=SPI_WriteByte(0x00);
   SpiDelay(10);

   SPI_TOUCH_CS_HIGH();

   x = x>>3;
   return (x);
}

u16 TPReadY(void)
{
 u16 y=0;

  SPI_TOUCH_CS_LOW();

  SpiDelay(10);

  SPI_WriteByte(0xD0);
  SpiDelay(10);
  y=SPI_WriteByte(0x00);
  y<<=8;
  y+=SPI_WriteByte(0x00);
  SpiDelay(10);

  SPI_TOUCH_CS_HIGH();

  y = y>>3;
  return (y);
}

// Mesure de la valeur de X et moyennage
int  GUI_TOUCH_X_MeasureX(void)
{
	unsigned char t=0,t1,count=0;
	unsigned short int databuffer[10]={5,7,9,3,2,6,4,0,3,1};
	unsigned short temp=0,X=0;

	while(count<10)
	{
		databuffer[count]=TPReadX();
		count++;
	}
	if(count==10)
	{
	    do
		{
			t1=0;
			for(t=0;t<count-1;t++)
			{
				if(databuffer[t]>databuffer[t+1])
				{
					temp=databuffer[t+1];
					databuffer[t+1]=databuffer[t];
					databuffer[t]=temp;
					t1=1;
				}
			}
		}while(t1);
		X=(databuffer[3]+databuffer[4]+databuffer[5])/3;
    }
	return(X);
}
// Mesure de la valeur de X et moyennage
int  GUI_TOUCH_X_MeasureY(void) {
  	unsigned char t=0,t1,count=0;
	unsigned short int databuffer[10]={5,7,9,3,2,6,4,0,3,1};
	unsigned short temp=0,Y=0;

    while(count<10)	
	{
		databuffer[count]=TPReadY();
		count++;
	}
	if(count==10)
	{
	    do
		{
			t1=0;
			for(t=0;t<count-1;t++)
			{
				if(databuffer[t]>databuffer[t+1])
				{
					temp=databuffer[t+1];
					databuffer[t+1]=databuffer[t];
					databuffer[t]=temp;
					t1=1;
				}
			}
		}while(t1);
		Y=(databuffer[3]+databuffer[4]+databuffer[5])/3;
	}
	return(Y);
}
